/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author damfercer
 */
@Entity
@Table(name = "candidate_professional_info")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "CandidateProfessionalInfo.findAll", query = "SELECT c FROM CandidateProfessionalInfo c"),
    @NamedQuery(name = "CandidateProfessionalInfo.findByIdCandidateProfessionalInfo", query = "SELECT c FROM CandidateProfessionalInfo c WHERE c.idCandidateProfessionalInfo = :idCandidateProfessionalInfo"),
    @NamedQuery(name = "CandidateProfessionalInfo.findBestProfessionalInfoFromCandidate", query = "SELECT c FROM CandidateProfessionalInfo c WHERE c.candidate = :candidate ORDER BY c.jobDuration.value DESC"),    
    @NamedQuery(name = "CandidateProfessionalInfo.findByCompany", query = "SELECT c FROM CandidateProfessionalInfo c WHERE c.company = :company")})
public class CandidateProfessionalInfo implements Serializable, Comparable<CandidateProfessionalInfo> {
    @Basic(optional = false)
    @NotNull
    @Column(name = "CURRENT")
    private boolean current;
    @Size(max = 150)
    @Column(name = "DESCRIPTION")
    private String description;
    @Column(name = "START_DATE")
    @Temporal(TemporalType.DATE)
    private Date startDate;
    @Column(name = "FINISH_DATE")
    @Temporal(TemporalType.DATE)
    private Date finishDate;
    @JoinColumn(name = "JOB_POSITION", referencedColumnName = "ID_JOB_POSITION")
    @ManyToOne(fetch = FetchType.LAZY)
    private JobPosition jobPosition;
    @JoinColumn(name = "SECTOR", referencedColumnName = "ID_SECTOR")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Sector sector;
    @JoinColumn(name = "CANDIDATE", referencedColumnName = "ID_CANDIDATE")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Candidate candidate;
    @JoinColumn(name = "JOB_DURATION", referencedColumnName = "ID_JOB_DURATION")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private JobDuration jobDuration;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID_CANDIDATE_PROFESSIONAL_INFO")
    private Integer idCandidateProfessionalInfo;
    @Size(max = 40)
    @Column(name = "COMPANY")
    private String company;

    public CandidateProfessionalInfo() {
    }

    public CandidateProfessionalInfo(Integer idCandidateProfessionalInfo) {
        this.idCandidateProfessionalInfo = idCandidateProfessionalInfo;
    }

    public Integer getIdCandidateProfessionalInfo() {
        return idCandidateProfessionalInfo;
    }

    public void setIdCandidateProfessionalInfo(Integer idCandidateProfessionalInfo) {
        this.idCandidateProfessionalInfo = idCandidateProfessionalInfo;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idCandidateProfessionalInfo != null ? idCandidateProfessionalInfo.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        boolean res=false;
        if (object instanceof CandidateProfessionalInfo) {
            CandidateProfessionalInfo other=(CandidateProfessionalInfo)object;
            if(this.getIdCandidateProfessionalInfo()!=null && other.getIdCandidateProfessionalInfo()!=null){
                res=this.getIdCandidateProfessionalInfo().equals(other.getIdCandidateProfessionalInfo());
            }
            else{
                if(this.getCompany()!=null && other.getCompany()!=null){
                    res=this.getSector().getIdSector().equals(other.getSector().getIdSector()) && this.getJobPosition().getIdJobPosition().equals(other.getJobPosition().getIdJobPosition()) && this.getCompany().equals(other.getCompany());
                }
                else{
                    res=this.getSector().getIdSector().equals(other.getSector().getIdSector()) && this.getJobPosition().getIdJobPosition().equals(other.getJobPosition().getIdJobPosition());
                }
            }
        }
        return res;
    }

    @Override
    public String toString() {
        return "entities.CandidateProfessionalInfo[ idCandidateProfessionalInfo=" + idCandidateProfessionalInfo + " ]";
    }

    public JobPosition getJobPosition() {
        return jobPosition;
    }

    public void setJobPosition(JobPosition jobPosition) {
        this.jobPosition = jobPosition;
    }

    public Sector getSector() {
        return sector;
    }

    public void setSector(Sector sector) {
        this.sector = sector;
    }

    public Candidate getCandidate() {
        return candidate;
    }

    public void setCandidate(Candidate candidate) {
        this.candidate = candidate;
    }

    public JobDuration getJobDuration() {
        return jobDuration;
    }

    public void setJobDuration(JobDuration jobDuration) {
        this.jobDuration = jobDuration;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getFinishDate() {
        return finishDate;
    }

    public void setFinishDate(Date finishDate) {
        this.finishDate = finishDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean getCurrent() {
        return current;
    }

    public void setCurrent(boolean current) {
        this.current = current;
    }

    @Override
    public int compareTo(CandidateProfessionalInfo cpi) {
        int res= 0;
        if(finishDate!=null && !current && cpi.finishDate!=null && !cpi.getCurrent()){
            res=finishDate.compareTo(cpi.finishDate);
        }
        else if(finishDate!=null && !current && cpi.finishDate==null && cpi.getCurrent()){
            res=-1;
        }
        else if(finishDate==null && current && cpi.finishDate!=null && !cpi.getCurrent()){
            res=1;
        }
        return res;
    }
}
